<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 5/26/21
 * Time: 5:18 PM
 */

namespace app\api\controller\v1;

use app\api\model\ShopOwner as ShopOwnerModel;
use app\api\model\ShopStaff as ShopStaffModel;

use app\api\model\User as UserModel;
use LinCmsTp5\exception\ParameterException;

// 店长
class ShopOwner
{
    /**
     * 获取店长列表
     * @validate('PagingParameter')
     */
    public function getDataList($name,$store_id,$page,$limit)
    {
        $where = [];
        if($name) {
            $where[] = ['name','like','%'.$name.'%'];
        }
        if($store_id) {
            $where[] = ['store_id', 'eq', $store_id];
        }
        $query = ShopOwnerModel::where($where)->with('store,user')->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 获取店长详情
     *  @validate('IdMustBePositiveInt')
     */
    public function getDataInfo($id)
    {
        return ShopOwnerModel::get($id);
    }


    /**
     * 创建店长信息
     * @validate('CheckStoreGroup')
     */
    public function createData($name,$phone,$desc,$store_id,$user_id)
    {
        ShopOwnerModel::create([
            'name' => $name,
            'phone' => $phone,
            'desc' => $desc,
            'store_id' => $store_id,
            'user_id' => $user_id
        ]);
        return writeJson(201, [], '添加成功！');
    }

    /**
     * 修改店长信息
     * @validate('CheckStoreGroup')
     */
    public function updateData($id,$name,$phone,$desc,$store_id,$user_id)
    {
        $info = ShopOwnerModel::where('id',$id)->find();
        $user = UserModel::where('id',$user_id)->find();
        if($info['user_id'] != $user['id']) {
            if ($user['type'] != 1) {
                throw new ParameterException([
                    'msg' => [
                        'shop_owner' => '该用户已经绑定角色'
                    ]
                ]);
            }
        }

        if($info) {
            $info->name = $name;
            $info->phone = $phone;
            $info->desc = $desc;
            $info->store_id = $store_id;
            $info->user_id = $user_id;
            $info->save();
            UserModel::where('id',$user_id)->update([
                'type'=> 2
            ]);
        }
        return writeJson(202, [], '修改成功！');
    }

    /**
     * 取消绑定
     */
    public function updateType($id)
    {
        $info = ShopOwnerModel::where('id',$id)->find();
        if($info) {
//            $user_id = $info['user_id'];
//            $info->user_id = null;
//            $info->save();
//            UserModel::where('id',$user_id)->update([
//                'type' => 1
//            ]);
            ShopOwnerModel::destroy($id);
            ShopStaffModel::create([
                'name' => $info['name'],
                'phone' => $info['phone'],
                'desc' => $info['desc'],
                'store_id' => $info['store_id'],
                'user_id' => $info['user_id'],
            ]);
            UserModel::where('id',$info['user_id'])->update([
                'type' => 3
            ]);
        }
        return writeJson(202, [], '切换成功！');
    }




    /**
     * 删除店长信息
     * @validate('IdMustBePositiveInt')
     */
    public function deleteData($id)
    {
        $info = ShopOwnerModel::where('id',$id)->find();
        if($info) {
            $user_id = $info['user_id'];
            ShopOwnerModel::destroy($id);
            UserModel::where('id',$user_id)->update([
                'type' => 1
            ]);
        }
        return writeJson(203, [], '删除成功！');
    }
}